package com.maimang.platform.quartz.service.impl;

import com.maimang.platform.constant.ScheduleConstants;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.quartz.entity.SysJob;
import com.maimang.platform.quartz.mapper.SysJobMapper;
import com.maimang.platform.quartz.service.SysJobService;
import com.maimang.platform.quartz.utils.CronUtils;
import com.maimang.platform.quartz.utils.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname SysJobServiceImpl
 * @description
 * @date 2019-04-16 19:36
 * @email jintaozhao@qq.com
 */
@Service
public class SysJobServiceImpl implements SysJobService {

	@Autowired
	private Scheduler scheduler;

	@Autowired
	private SysJobMapper sysJobMapper;

	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init() {
		List<SysJob> jobList = sysJobMapper.selectJobAll();
		for (SysJob job : jobList) {
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, job.getJobId());
			// 如果不存在，则创建
			if (cronTrigger == null) {
				ScheduleUtils.createScheduleJob(scheduler, job);
			} else {
				ScheduleUtils.updateScheduleJob(scheduler, job);
			}
		}
	}


	/**
	 * 获取quartz调度器的计划任务
	 *
	 * @param job 调度信息
	 * @return 调度任务集合
	 */
	@Override
	public List<SysJob> selectJobList(SysJob job) {
		return sysJobMapper.selectJobList(job);
	}

	/**
	 * 通过调度任务ID查询调度信息
	 *
	 * @param jobId 调度任务ID
	 * @return 调度任务对象信息
	 */
	@Override
	public SysJob selectJobById(Long jobId) {
		return sysJobMapper.selectJobById(jobId);
	}

	/**
	 * 暂停任务
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int pauseJob(SysJob job) {
		job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
		int rows = sysJobMapper.updateJob(job);
		if (rows > 0) {
			ScheduleUtils.pauseJob(scheduler, job.getJobId());
		}
		return rows;
	}

	/**
	 * 恢复任务
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int resumeJob(SysJob job) {
		job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
		int rows = sysJobMapper.updateJob(job);
		if (rows > 0) {
			ScheduleUtils.resumeJob(scheduler, job.getJobId());
		}
		return rows;
	}

	/**
	 * 删除任务后，所对应的trigger也将被删除
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int deleteJob(SysJob job) {
		int rows = sysJobMapper.deleteJobById(job.getJobId());
		if (rows > 0) {
			ScheduleUtils.deleteScheduleJob(scheduler, job.getJobId());
		}
		return rows;
	}

	/**
	 * 批量删除调度信息
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public void deleteJobByIds(String ids) {
		Long[] jobIds = ConvertUtils.toLongArray(ids);
		for (Long jobId : jobIds) {
			SysJob job = sysJobMapper.selectJobById(jobId);
			deleteJob(job);
		}
	}

	/**
	 * 任务调度状态修改
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int changeStatus(SysJob job) {
		int rows = 0;
		String status = job.getStatus();
		if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
			rows = this.resumeJob(job);
		} else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
			rows = this.pauseJob(job);
		}
		return rows;
	}

	/**
	 * 立即运行任务
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int run(SysJob job) {
		return ScheduleUtils.run(scheduler, selectJobById(job.getJobId()));
	}

	/**
	 * 新增任务表达式
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int insertJobCron(SysJob job) {
		job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
		int rows = sysJobMapper.insertJob(job);
		if (rows > 0) {
			ScheduleUtils.createScheduleJob(scheduler, job);
		}
		return rows;
	}

	/**
	 * 更新任务的时间表达式
	 *
	 * @param job 调度信息
	 * @return 结果
	 */
	@Override
	public int updateJobCron(SysJob job) {
		int rows = sysJobMapper.updateJob(job);
		if (rows > 0) {
			ScheduleUtils.updateScheduleJob(scheduler, job);
		}
		return rows;
	}

	/**
	 * 校验cron表达式是否有效
	 *
	 * @param cronExpression 表达式
	 * @return 结果
	 */
	@Override
	public boolean checkCronExpressionIsValid(String cronExpression) {
		return CronUtils.isValid(cronExpression);
	}
}
